/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yonsei.iwb.sdt.datastructure;

import org.yonsei.iwb.sdt.datastructure.tree.DNode;
import com.hp.hpl.jena.rdf.model.Resource;
import java.util.NoSuchElementException;
import java.util.Vector;
import org.yonsei.iwb.sdt.ontology.PropertyArc;
import org.yonsei.iwb.sdt.util.UtilFunctions;

/**
 *
 * @author JDK
 */
public class DecomposeCandidateSet extends Vector<DecomposeCandidate> {

//    Resource centerClass;
    private Vector<Resource> targetclass;
    Vector<PropertyArc> propertySet;

    public DecomposeCandidateSet() {
        this.targetclass = new Vector<Resource>();
    }

    public void setTargetclass(Resource targetclass) {
        this.targetclass.add(targetclass);
    }

    public Resource getLastAddedTargetClass() {
        try {
            return this.targetclass.lastElement();
        } catch (NullPointerException npe) {
            return null;
        } catch (NoSuchElementException nsee){
            return null;
        }
    }

    public void setPropertySet(PropertyArc p) {
        if (propertySet == null) {
            this.propertySet = new Vector<PropertyArc>();
            this.propertySet.add(p);
        } else {
            this.propertySet.add(p);
        }
    }

    public void setPropertySet(Vector<PropertyArc> propertySet) {
        try {
            if (propertySet.isEmpty()) {
                this.propertySet = new Vector<PropertyArc>();
            } else {
                this.propertySet = new Vector<PropertyArc>();
                for (int i = 0; i < propertySet.size(); i++) {
                    this.propertySet.add(propertySet.get(i));

                }
            }
        } catch (NullPointerException npe) {
        }
    }

    public Vector<PropertyArc> getPropertySet() {
        return this.propertySet;
    }

//    public void setCenterClass(Resource centerClass) {
//        this.centerClass = centerClass;
//    }
//
//    public Resource getCenterClass() {
//        return this.centerClass;
//    }

    public void calculateInformationGain(DNode parentNode, TargetInstanceSet tivi) {

        double pnodeEntropy = UtilFunctions.calculateEntropy(parentNode.getTargetInstanceValueInformation());
        int pnodeInsSize = parentNode.getInstanceSetSize();
        for (int i = 0; i < this.size(); i++) {
            DecomposeCandidate dc = this.get(i);
            Vector<DNode> childNodeSet = dc.getChildNodeSet();
            double sumofEntropy = 0.0;
//            dc.printDecomposeCandidateDetail();
            for (int j = 0; j < childNodeSet.size(); j++) {
                DNode childnode = childNodeSet.get(j);
                childnode.setTargetValueInstanceInformation(tivi);
                childnode.setEntropy();
                double cnodeEntropy = childnode.getEntropy();
                double childnodesize = childnode.getInstanceSetSize();
                double outofsize = childnodesize / pnodeInsSize;
//                System.out.println("----------  " + outofsize);
                sumofEntropy = sumofEntropy + outofsize * cnodeEntropy;

            }

            double n = pnodeEntropy - sumofEntropy;
//            System.out.println("information gain = pnodeE: " +pnodeEntropy+" - cnodeE: "+sumofEntropy+" = "+n);
            dc.setInformation_Gain(pnodeEntropy - sumofEntropy);
//            dc.printDecomposeCandidateDetail();
        }
    }

    public void printDecomposeCandidateSet() {
        for (int i = 0; i < this.size(); i++) {
            DecomposeCandidate dc = this.get(i);
            dc.printDecomposeCandidateDetail();

        }
    }
}
